﻿// Copyright 2016 Intel Corporation.

// The source code, information and material ("Material") contained herein is
// owned by Intel Corporation or its suppliers or licensors, and title to such
// Material remains with Intel Corporation or its suppliers or licensors. The
// Material contains proprietary information of Intel or its suppliers and
// licensors. The Material is protected by worldwide copyright laws and treaty
// provisions. No part of the Material may be used, copied, reproduced, modified,
// published, uploaded, posted, transmitted, distributed or disclosed in any way
// without Intel's prior express written permission. No license under any patent,
// copyright or other intellectual property rights in the Material is granted to
// or conferred upon you, either expressly, by implication, inducement, estoppel
// or otherwise. Any license under such intellectual property rights must be
// express and approved by Intel in writing.

// Include any supplier copyright notices as supplier requires Intel to use.

// Include supplier trademarks or logos as supplier requires Intel to use,
// preceded by an asterisk. An asterisked footnote can be added as follows:
// *Third Party trademarks are the property of their respective owners.

// Unless otherwise agreed by Intel in writing, you may not remove or alter this
// notice or any other notice embedded in Materials by Intel or Intel's suppliers
// or licensors in any way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Security;
using PasswordManager;

namespace Password_manager
{
    /// <summary>
    /// Interaction logic for ChangePassword.xaml
    /// </summary>
    public partial class ChangePassword : Window
    {
        PasswordManagerCore mgr;
        public int errorCode { get; set; }

        public ChangePassword(PasswordManagerCore mgr_in)
        {
            mgr = mgr_in;
            errorCode = PasswordManagerStatus.UserCancelled;

            InitializeComponent();
        }

        private void btnPassSave_Click(object sender, RoutedEventArgs e)
        {          
            errorCode = mgr.change_master_password(txtOldPass.SecurePassword, txtNewPass.SecurePassword, txtConfirmPass.SecurePassword);

            txtOldPass.SecurePassword.Clear();
            txtNewPass.SecurePassword.Clear();
            txtConfirmPass.SecurePassword.Clear();

            if ( errorCode == PasswordManagerStatus.OK ) 
            {
                this.DialogResult = true;                
            } else if ( errorCode == PasswordManagerStatus.Mismatch )
            {
                MessageBox.Show("New password and confirmed password do not match", "Passwords do not match", MessageBoxButton.OK);
                return;
            } else
            {
                this.DialogResult = false;
            }

            this.Close();      
        }

        private void btnPassCancel_Click(object sender, RoutedEventArgs e)
        {
            txtOldPass.SecurePassword.Clear();
            txtNewPass.SecurePassword.Clear();
            txtConfirmPass.SecurePassword.Clear();

            this.DialogResult = false;
            this.Close();
        }
    }
}
